package com.ikangtai.shecare.personal;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.ikangtai.shecare.R;
import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.BaseActivity;
import com.ikangtai.shecare.common.baseView.TopBar;
import com.ikangtai.shecare.common.util.PollingUtil;
import com.jaeger.library.StatusBarUtil;
import com.kyleduo.switchbutton.SwitchButton;

public class SettingsActivity extends BaseActivity implements View.OnClickListener {
    public static final String kPlanNotiTime = "planNotiTime";
    public static final String kDailyNoti = "dailyNoti";

    private String timer = "20:00";
    private LayoutInflater inflater;
    private TopBar topBar;
    private Button logout_button;
    private RelativeLayout alert_repeat;
    private RelativeLayout alert;
    private TextView alarm_text;
    private SwitchButton switchButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_settings);
        inflater = LayoutInflater.from(SettingsActivity.this);
        StatusBarUtil.setColor(this, getResources().getColor(R.color.orange), 0);
        initView();
    }

    private void initView() {
        topBar = (TopBar) findViewById(R.id.topBar);
        topBar.setOnTopBarClickListener(new TopBar.OnTopBarClickListener() {
            @Override
            public void leftClick() {
                finish();
            }

            @Override
            public void midLeftClick() {

            }

            @Override
            public void midRightClick() {

            }

            @Override
            public void rightClick() {

            }
        });

        logout_button = (Button) findViewById(R.id.logout_button);
        logout_button.setOnClickListener(this);
        alert_repeat = (RelativeLayout) findViewById(R.id.alertRepeat);
        alert = (RelativeLayout) findViewById(R.id.alert);
        alert_repeat.setOnClickListener(this);
        alert.setOnClickListener(this);
        switchButton = (SwitchButton) findViewById(R.id.alarm_button);
        switchButton.setThumbColorRes(R.color.white);
        alarm_text = (TextView) findViewById(R.id.alarm_text);
        alarm_text.setText(App.getPreference(kPlanNotiTime).equals("") ? "10:00" : App.getPreference(kPlanNotiTime));

        switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    App.savePreference(kDailyNoti, true);
                    switchButton.setBackColorRes(R.color.orange);
                    switchButton.setThumbColorRes(R.color.white);
                    PollingUtil.startPollingService(SettingsActivity.this, PollingUtil.NOTIFICATION_PLAN,
                            PollingService.DAY_MILLISECONDS, PollingService.class, PollingService
                                    .ACTION);
                } else {
                    switchButton.setTintColor(R.color.white);
                    App.savePreference(kDailyNoti, false);
                    PollingUtil.stopPollingService(SettingsActivity.this, PollingUtil.NOTIFICATION_PLAN,
                            PollingService.class, PollingService.ACTION);
                }
            }
        });
        switchButton.setChecked(App.getPreferenceBoolean(kDailyNoti));
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.logout_button:
                App.saveUserPreference(App.SHARED_PREF_USER_NAME, null);
                Intent intent = new Intent(SettingsActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
                App.clearAllActivities();
                break;
            case R.id.alertRepeat:
                Intent alertRepeatIntent = new Intent(SettingsActivity.this, AlertRepeatListActivity.class);
                startActivity(alertRepeatIntent);
                break;
            case R.id.alert:
                final View layout = inflater.inflate(R.layout.activity_setting_datetime, null)
                        .findViewById(R.id.setting_time_dialog);
                final TimePicker timePicker;
                timePicker = (TimePicker) layout.findViewById(R.id.dialog_setting_date);
                timePicker.setIs24HourView(true);
                if (App.getPreference(kPlanNotiTime).equals("")) {
                } else {
                    timePicker.setCurrentHour(Integer.parseInt(App.getPreference(kPlanNotiTime).split(":")[0]));
                    timePicker.setCurrentMinute(Integer.parseInt(App.getPreference(kPlanNotiTime).split(":")[1]));
                }
                timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
                    @Override
                    public void onTimeChanged(TimePicker timePicker, int i, int i1) {
                        if (i >= 10 && i1 >= 10) {
                            timer = (i + ":" + i1);
                        } else if (i >= 10 && i1 < 10) {
                            timer = (i + ":0" + i1);
                        } else if (i < 10 && i1 >= 10) {
                            timer = ("0" + i + ":" + i1);
                        } else {
                            timer = ("0" + i + ":0" + i1);
                        }

                    }
                });
                AlertDialog dialog = new AlertDialog.Builder(inflater.getContext()).setTitle(R.string.setting_reminder).setView(layout)
                        .setPositiveButton(R.string.sure, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                App.savePreference(kPlanNotiTime, timer);
                                alarm_text.setText(App.getPreference(kPlanNotiTime));
                                if (switchButton.isChecked()) {
                                    PollingUtil.startPollingService(SettingsActivity.this, PollingUtil.NOTIFICATION_PLAN,
                                            PollingService.DAY_MILLISECONDS, PollingService.class, PollingService
                                                    .ACTION);
                                }
                            }
                        }).setNegativeButton(R.string.cancel, null).create();

                dialog.show();
                break;

        }
    }
}
